package com.tucc.hello.leetcode.no1_400.no1_200.no61_80.no61;

import com.tucc.hello.leetcode.common.ListNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {};
        for (int i = 0; i < nums.length; i++) {
            ListNode listNode = ListNode.of(nums);
            ListNode.print(solution.rotateRight(listNode, i));
        }
        ListNode listNode = ListNode.of(nums);
        ListNode.print(solution.rotateRight(listNode, 1));
    }

    public ListNode rotateRight(ListNode head, int k) {
        if (head == null) {
            return head;
        }
        List<ListNode> nodes = new ArrayList<>();
        ListNode tail = head;
        while (tail != null) {
            nodes.add(tail);
            tail = tail.next;
        }
        k = k % nodes.size();
        if (k == 0 || nodes.size() <= 1) {
            return head;
        }
        int headIndex = nodes.size() - k;
        nodes.get(headIndex - 1).next = null;
        nodes.get(nodes.size() - 1).next = nodes.get(0);
        return nodes.get(headIndex);
    }

}
